package com.scalar.chiptrack.operations.processtypesetup.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.processtypesetup.dao.ProcessTypeSetupDAO;
import com.scalar.chiptrack.operations.processtypesetup.forms.ProcessTypeListForm;


public final class ProcessTypeListAction extends ChipTrackAction 
{
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
	{
		HashMap errors 			= new HashMap();
		HashMap info 			= new HashMap();
		ArrayList processList 	= new ArrayList();
		ProcessTypeListForm processSearchForm	= ( ProcessTypeListForm ) form;
				
		try 
		{
            if( "SearchProcess".equals( processSearchForm.getAction() ) ) 
            {
				processList 	= ProcessTypeSetupDAO.getProcesses( processSearchForm.getSearchProcessName(), processSearchForm.getSearchProcessType() );
				if( processList != null && processList.isEmpty() == false )
				{
					request.setAttribute( "processList", processList );
				}
				else
				{
					if( processSearchForm.getSearchProcessName() != null && processSearchForm.getSearchProcessName().length() > 0 )
					{
						if( processSearchForm.getSearchProcessType() != null && processSearchForm.getSearchProcessType().length() > 0 )
						{
							errors.put( "There is no Process with Process Name ", processSearchForm.getSearchProcessName() +" and Process Type : "+ processSearchForm.getSearchProcessType() );		
						}
						else
						{
							errors.put( "There is no Process with Process Name ", processSearchForm.getSearchProcessName() );
						}
					}
					else if( processSearchForm.getSearchProcessType() != null && processSearchForm.getSearchProcessType().length() > 0 )
					{
						errors.put( "There is no Process with Process Type ", processSearchForm.getSearchProcessType() );
					}
					else
					{
						errors.put( "No records Found ", null );
					}
				}
				
	            if ( !errors.isEmpty() ) 
	            {
					request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
				}
            }

		} catch(Exception ex) {}
		return (mapping.findForward("success"));
    }
}